/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 */

import React, { Component } from 'react';
import {
  Platform,
  StyleSheet,
  Text,
  ScrollView,
  Dimensions,
  SafeAreaView,
  View
} from 'react-native';
import Home from './Home'
import Insurance from './Insurance'
import Shop from './Shop'
import Community from './Community'

import TabNavigator from 'react-native-tab-navigator';
import Icon from 'react-native-vector-icons/FontAwesome';

const instructions = Platform.select({

  ios: 'Press Cmd+R  reload,\n' +
    'Cmd+D or shake for dev menu',
  android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});

type State = {
  selectedTab: string
};

const deviceW = Dimensions.get('window').width

const basePx = 375

function px2dp(px) {
  return px *  deviceW / basePx
}


class Profile extends Component<{}> {
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Profile
        </Text>
      </View>
    )
  }
}
export default class App extends Component<{}, State> {
  state : State = {
    selectedTab: 'home'
  };
  render() {
    return (
      <TabNavigator style={styles.container}>
        <TabNavigator.Item
          selected={this.state.selectedTab === 'home'}
          title="首页"
          selectedTitleStyle={{color: "#3496f0"}}
          renderIcon={() => <Icon name="home" size={px2dp(22)} color="#666"/>}
          renderSelectedIcon={() => <Icon name="home" size={px2dp(22)} color="#3496f0"/>}
          badgeText="1"
          onPress={() => this.setState({selectedTab: 'home'})}>
          <Home/>
        </TabNavigator.Item>
        <TabNavigator.Item
          selected={this.state.selectedTab === 'insurance'}
          title="保险"
          selectedTitleStyle={{color: "#3496f0"}}
          renderIcon={() => <Icon name="money" size={px2dp(22)} color="#666"/>}
          renderSelectedIcon={() => <Icon name="money" size={px2dp(22)} color="#3496f0"/>}
          onPress={() => this.setState({selectedTab: 'insurance'})}>
          <Insurance/>
        </TabNavigator.Item>
        <TabNavigator.Item
          selected={this.state.selectedTab === 'shop'}
          title="商城"
          selectedTitleStyle={{color: "#3496f0"}}
          renderIcon={() => <Icon name="shopping-cart" size={px2dp(22)} color="#666"/>}
          renderSelectedIcon={() => <Icon name="shopping-cart" size={px2dp(22)} color="#3496f0"/>}
          onPress={() => this.setState({selectedTab: 'shop'})}>
          <Insurance/>
        </TabNavigator.Item>
        <TabNavigator.Item
          selected={this.state.selectedTab === 'community'}
          title="社区"
          selectedTitleStyle={{color: "#3496f0"}}
          renderIcon={() => <Icon name="group" size={px2dp(22)} color="#666"/>}
          renderSelectedIcon={() => <Icon name="group" size={px2dp(22)} color="#3496f0"/>}
          onPress={() => this.setState({selectedTab: 'community'})}>
          <Community/>
        </TabNavigator.Item>
        <TabNavigator.Item
          selected={this.state.selectedTab === 'profile'}
          title="Profile"
          selectedTitleStyle={{color: "#3496f0"}}
          renderIcon={() => <Icon name="user" size={px2dp(22)} color="#666"/>}
          renderSelectedIcon={() => <Icon name="user" size={px2dp(22)} color="#3496f0"/>}
          onPress={() => this.setState({selectedTab: 'profile'})}>
          <Profile/>
        </TabNavigator.Item>
      </TabNavigator>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
    // backgroundColor: 'red',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  instructions: {
    textAlign: 'center',
    color: '#333333',
    marginBottom: 5,
  },
});
